<?php

namespace app\index\controller;

use app\admin\model\DepartmenModel;
use app\index\model\Fabulous;
use app\index\model\Time;
use think\Controller;
use think\Db;
use think\Request;

class Rankings extends Controller
{
    /**
     * 点赞
     */
    public function fabulous(Request $request)
    {
//        Cache::clear();
//        return 1;
        //return 1;
        //被点赞人id
        $be_praised_id = $request->param('id',null);
        if(!$be_praised_id){
            return returns(405,'你已经点过赞了');
        }
        //return $be_praised_id;
        if(!$user =isSign()){
            return returns();
        }
        //当前登录人id
        $thumbs_up = $user['id'];
        $data = Fabulous::dianzhan($thumbs_up,$be_praised_id);
//        return $data;
        if ($data) {
            return json(['code' => 200, 'msg' => '点赞成功']);
        } else {
            return json(['code' => 405, 'msg' => '点赞失败，你已点赞了']);
        }

    }

    /**获取某公司的部门列表
     * @param Request $request
     * @return \think\response\Json
     */
    public function departmenList()
    {
        $company_id = session("user")["company_id"];
        $departmen = Db::name("departmen_table")->where("company_id", $company_id)->select();
        return json(["code" => 200, "departmen" => $departmen]);
    }


    /**其他部门
     * @param Request $request
     */
    public function otherdepartment(Request $request)
    {
        //部门排行
        if(!$user=isSign()){
            return returns();
        }
        $id = session("user.id");
        //公司id
        $company_id = session("user")['company_id'];
        //部门id
        $department_id = $request->param("id");
        //部门排行
        $bumeninfo = Db::name("user")
            ->alias('u')
            ->join(['zhiu_departmen_table' => 'd'], "d.id=u.department_id")
            ->order("integral desc")
            ->field('u.*,d.departmen_name')
            ->where("u.company_id", $company_id)
            ->where("u.department_id", $department_id)
            ->select();
        $bumen = DepartmenModel::getDepartmen()
            ->where('departmen_table.id', $department_id)
            ->find();
        foreach ($bumeninfo as $k=>$value){
            if(cache($user['id'].'点赞'.$value['id'])){
                $bumeninfo[$k]['zan'] = 1;
            }else{
                $bumeninfo[$k]['zan'] = 0;
            }
        }
        //return json($bumeninfo);
        //当前用户
        $user = Db::name("user")
            ->alias('u')
            ->join(['zhiu_departmen_table' => 'd'], "d.id=u.department_id")
            ->order("integral desc")
            ->field('u.*,d.departmen_name')
            ->select();
        //获取当前用户是属于第几位
        foreach ($user as $k => $v) {
            if ($v["id"] == $id) {
                $my = $k + 1;
                $one[] = $v;
            }
        }
        return json(['data' => $bumeninfo, 'bumen' => $bumen, "user" => $user]);
        //点赞
//        $zan = Db::name("fabulous")->where('be_praised', $id)->field("thumbs_up")->select();
    }

    /**
     *  查询评论
     */
    public function msg(Request $request)
    {
        if(!$user=session('user')){
            return $this->redirect('/index/index/login');
        }
        session("bumen_id",$request->param("bumen"));
//        echo session("bumen_id");
        //公司id
        $company_id = session("user")['company_id'];
        //部门id
        $department_id = session("user")["department_id"];
        //判断是公司还是部门评论
        if(empty(session("bumen_id"))){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id desc")
                ->paginate(5)
                ->toArray();
        }elseif(session("bumen_id")=='x'){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",$department_id)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id desc")
                ->paginate(5)
                ->toArray();
        }elseif(session("bumen_id")!='x'){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",session("bumen_id"))
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id desc")
                ->paginate(5)
                ->toArray();
        }

        cache($user['id'].'pinglun',time());
        //$company_comments = array();
        //将时间戳转换为（多少分钟前，几天前格式）
        foreach ($comments['data'] as $k => $v) {
            $x = Time::tranTime($v["create_time"]);
            $comments['data'][$k]['shijian'] = $x;
        }
//echo '<pre>';
//        print_r($comments);exit;
        return view("index/msg", ["company_comments" => $comments]);
    }

    /**
     *  添加评论
     */
    public function createcomment(Request $request)
    {
        //公司id
        $company_id = session("user")["company_id"];
        //部门id
        $department_id = session("user")["department_id"];

        if(session("bumen_id")!= $department_id && session("bumen_id")!= 'x' && !empty(session("bumen_id"))){
            return json(["code"=> 404]);
        }
        //拼接字段增加评论 state
        $data = [];
        $data["userid"] = session("user")["id"];
        $data["company_id"] = session("user")["company_id"];
        $data["department_id"] = session("user")["department_id"];
        $data["create_time"] = time();
        $data["contents"] = $request->param("cont");

        $id = Db::name("comment")->InsertGetId($data);
        $one_li = $request->param("one_li");
        //拼接字段增加评论 end

        //判断是公司还是部门评论
        if(empty(session("bumen_id"))){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.id",">",$one_li)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id asc")
                ->select();
        }elseif(session("bumen_id")=='x'){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",$department_id)
                ->where("c.id",">",$one_li)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id asc")
                ->select();
        }elseif(session("bumen_id")==$department_id){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",$department_id)
                ->where("c.id",">",$one_li)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id asc")
                ->select();
        }
        $count = Db::name("comment")
            ->where("company_id",$company_id)
            ->where("department_id",$department_id)
            ->count();
        if ($id) {
            return json(["code" => 200,"comments"=>$comments,'count'=>$count]);
        } else {
            return json(["code" => 401, "msg" => "您的评论被小怪兽吃掉了，请稍后再试"]);
        }
    }

    /***
     *  下拉刷新
     */
    public function shuaxin(Request $request)
    {
        if(!$user=session('user')){
            return json(['code'=>401,'msg'=>'请登录']);
        }
//        return json($request->param());
//        $department_id = $request->param('$request_id',null);
//        if($department_id){
//
//        }else{
//
//        }
        //公司id
        $company_id = session("user")['company_id'];
        //部门id
        $department_id = session("user")["department_id"];
        if(empty(session("bumen_id"))){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where('c.create_time','>',cache($user['id'].'pinglun'))
                ->field('c.*,d.departmen_name,u.nickname')
                ->order("c.id desc")
                ->select();
            $count = Db::name("comment")
                ->where("company_id",$company_id)
                ->count();
        }elseif(session("bumen_id")=="x"){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",$department_id)
                ->where('c.create_time','>',cache($user['id'].'pinglun'))
                ->field('c.*,d.departmen_name,u.nickname')
                ->order("c.id desc")
                ->select();
            $count = Db::name("comment")
                ->where("company_id",$company_id)
                ->where('department_id',$department_id)
                ->count();
        }elseif(session("bumen_id")!='x'){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",session("bumen_id"))
                ->where('c.create_time','>',cache($user['id'].'pinglun'))
                ->field('c.*,d.departmen_name,u.nickname')
                ->order("c.id desc")
                ->select();
            $count = Db::name("comment")
                ->where("company_id",$company_id)
                ->where('department_id',session("bumen_id"))
                ->count();
        }

        $company_comments = array();
        //将时间戳转换为（多少分钟前，几天前格式）
        foreach ($comments as $k => $v) {
            $x = Time::tranTime($v["create_time"]);
            $company_comments[] = $v;
            $company_comments[$k]['shijian'] = $x;
        }

        return json(["code" => 200, "data" => $company_comments,'count'=>$count]);
    }

    /***
     * 上拉加载
     */
    public function upload(Request $request)
    {
        //最后一条数据的id
        $list_id = $request->param("list_id");
        //公司id
        $company_id = session("user")['company_id'];
        //部门id
        $department_id = session("user")["department_id"];
        if(empty(session("bumen_id"))){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.id","<",$list_id)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id desc")
                ->limit(5)
                ->select();
            $count = Db::name("comment")
                ->where("company_id",$company_id)
                ->count();
        }elseif(session("bumen_id")=='x'){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",$department_id)
                ->where("c.id","<",$list_id)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id desc")
                ->limit(5)
                ->select();
            $count = Db::name("comment")
                ->where("company_id",$company_id)
                ->where('department_id',$department_id)
                ->count();
        }elseif(session("bumen_id")!='x'){
            $comments = Db::table("zhiu_comment")
                ->alias('c')
                ->join(['zhiu_departmen_table' => 'd'], "d.id=c.department_id")
                ->join(["zhiu_user" => "u"], "u.id=c.userid")
                ->where("c.company_id", $company_id)
                ->where("c.department_id",session("bumen_id"))
                ->where("c.id","<",$list_id)
                ->field('c.*,d.departmen_name,u.nickname,u.portrait')
                ->order("id desc")
                ->limit(5)
                ->select();
            $count = Db::name("comment")
                ->where("company_id",$company_id)
                ->where('department_id',session("bumen_id"))
                ->count();
        }

        $company_comments = array();
//        //将时间戳转换为（多少分钟前，几天前格式）
        foreach ($comments as $k => $v) {
            $v["create_time"] = Time::tranTime($v["create_time"]);
            $company_comments[] = $v;
        }
        foreach($company_comments as $k => $v){
            if($list_id == $v["id"]){
                return json(["code"=>401]);
            }else{
                return json(["code" => 200,"count"=>$count,"company_comments"=>$company_comments]);
            }

        }

    }

}